package net.comic.services

import net.comic.entities.*
import net.comic.controllers.*
import net.comic.enums.FinderType


class FinderService {
    boolean transactional = true


    def search(type, value) {
    	def results = []
        switch(Enum.valueOf(FinderType, type)){
		    case FinderType.CL:
		    	log.error "se busca por colleccion"
		    	def c = CollectionComic.createCriteria()
		    	results = c.list {
		    		ilike("name", "%"+value+"%")
		    		disabled == false
		    		maxResults(100)
		    		order("name", "desc")
		    	}
		    	break;

		    case FinderType.CO:
		    	log.error "se busca por comic"
		    	def c = Comic.createCriteria()
		    	results = c.list {
		    		ilike("name", "%"+value+"%")
		    		disabled == false
		    		maxResults(100)
		    		order("name", "desc")
		    	}
		    	break;

            case FinderType.US:
		    	log.error "se busca por usuario"
		    	def c = User.createCriteria()
		    	results = c.list {
		    		ilike("name", "%"+value+"%")
		    		disabled == false
		    		maxResults(100)
		    		order("name", "desc")
		    	}
		    	break;
		    	
            case FinderType.TA:
		    	log.error "se busca por tags"
		    	def c = Tag.createCriteria()
		    	results = c.list {
		    		ilike("name", "%"+value+"%")
		    		disabled == false
		    		maxResults(100)
		    		order("name", "desc")
		    	}
		    	break;

		    default: log.error "se busca por un tipo inexistente"
        }

        log.error "El numero de resultados encontrados para la busqueda: ${value} y el tipo: ${type} han producido ${results.size()} resultados"
        return results
    }
    
    def finder() {
    }
    
    def countFinder(value) {
    	println "size " + search("CO", value).size()
    	return search("CO", value)
    }
}
